package com.example.demo.controller;

import com.example.demo.model.ActivityDetail;
import com.example.demo.model.LoginMessage;
import com.example.demo.model.RegistrationMessage;
import com.example.demo.service.ActivityDetailService;
import com.example.demo.service.MessagingService;
import com.example.demo.service.RedisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/activity")
public class ActivityDetailController {

    @Resource
    ActivityDetailService activityDetailService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    MessagingService messagingService;


    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public ActivityDetail addActivityDetail(@RequestBody ActivityDetail activityDetail){
        activityDetailService.add(activityDetail);
        return activityDetail;
    }



    @RequestMapping(value = "/hello/{id}")
    public String hello(@PathVariable(value = "id") String id){
        //查询缓存中是否存在
        boolean hasKey = redisUtils.hasKey(id);
        String str = "zhulingjuan";
        if(hasKey){
            //获取缓存
            Object object =  redisUtils.get(id);
            str = object.toString();
        }else{
            //从数据库中获取信息
            //数据插入缓存（set中的参数含义：key值，user对象，缓存存在时间10（long类型），时间单位）
            redisUtils.setex(id,str,10L);
        }
        return str;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/rabbitmq")
    public String testRabbitMq(){
        messagingService.sendLoginMessage(new LoginMessage(false,"zhulingjuan","111"));
        messagingService.sendRegistrationMessage(new RegistrationMessage("zhulingjuan","30","女"));
        return "success";
    }
}
